<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>表格Demo</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../assets/libs/layui/css/layui.css" media="all">
</head>

<body>

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>添加食谱</legend>
    </fieldset>

    <form class="layui-form" action="" lay-filter="example" method="POST" action="" style="padding: 10px 50px;">
        <div class="layui-form-item">
            <label class="layui-form-label">食谱名称</label>
            <div class="layui-input-block">
                <input type="text" name="menuName" id="menuName" lay-verify="required" placeholder="请输入" required
                    autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">食谱标签</label>
            <div class="layui-input-block" id="menutag">
                <div class="layui-btn-container tag" lay-filter="demotag" lay-newTag="true" style="padding-top: 5px;">
                </div>
            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 20px">
            <label class="layui-form-label">食谱类型</label>
            <div class="layui-input-block">
                <select name="menuType" id='menuType'>

                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">食材价格</label>
            <div class="layui-input-block">
                <input type="text" name="menuPrice" id="menuPrice" lay-verify="required" placeholder="请输入" type="number"
                    autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">菜谱热量</label>
            <div class="layui-input-block">
                <input type="text" name="menuHot" id="menuHot" lay-verify="required" placeholder="请输入" type="number"
                    autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 20px">
            <label class="layui-form-label">食谱类型</label>
            <div class="layui-input-block">
                <select name="menuFlavor" id='menuFlavor'>
                    <option value="清淡">清淡</option>
                    <option value="微辣">微辣</option>
                    <option value="中辣">中辣</option>
                    <option value="麻辣">麻辣</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 20px">
            <label class="layui-form-label">食谱时区</label>
            <div class="layui-input-block">
                <select name="menuTime" id='menuTime'>
                    <option value="早餐">早餐</option>
                    <option value="午餐">午餐</option>
                    <option value="晚餐">晚餐</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">发布时间</label>
            <div class="layui-input-block">
                <input name="menuDate" id="menuDate" required lay-verify="required" placeholder="请输入" required
                    class="layui-input"></input>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">食谱简介</label>
            <div class="layui-input-block">
                <textarea name="menuComm" id="menuComm" required lay-verify="required" placeholder="请输入"
                    class="layui-textarea"></textarea>
            </div>
        </div>


        <div class="layui-upload">
            <button type="button" class="layui-btn" id="uploadBtn">食谱图片</button>
            <div class="layui-upload-list" id="previewBox">

            </div>
        </div>


        <div class="layui-form-item" style="margin-top: 20px;">
            <div class="layui-form-label">食谱步骤</div>
            <textarea id="menuMaterials" required lay-verify="required" placeholder="请输入"
                name="menuMaterials"></textarea>
        </div>


        <div class="layui-form-item" style="margin-top: 40px;">
            <div class="layui-input-block">
                <!-- lay-submit="" -->
                <button type="button" class="layui-btn" id="submit" lay-filter="demo1">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
    <style>
        .layui-upload-list {
            padding: 10px 40px;
            display: flex;
            min-height: 150px;
            min-width: 150px;
            border: 1px solid #cccccc;
        }

        .imgitem {
            cursor: pointer;
            background-color: #FF5722;
            ;
            width: 100%;
            text-align: center;
        }

        .imgBox {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            margin: 0px 20px;
        }
    </style>
    <script src="../../assets/libs/layui/layui.js" charset="utf-8"></script>
    <script src="../../assets/js/ace/ace.js"></script>
    <link rel="stylesheet" href="../../module/tag/tag.css">
    <script>

        layui.config({
            base: '/module/' //假设这是你存放拓展模块的根目录
        }).extend({ //设定模块别名
            tag: 'tag/tag' //如果 tag.js 是在根目录，也可以不用设定别名
        }).use(['upload', 'tag', 'config', 'form', 'jquery', 'upload', 'laydate', 'layedit'], function () {
            var tag = layui.tag;
            var config = layui.config;
            var form = layui.form;
            var $ = layui.jquery;
            var upload = layui.upload;
            var laydate = layui.laydate;
            var layedit = layui.layedit;
            var srcArr = new Array();
            var imgUrl = ''
            var files = [];
            tag.render("demotag", {
                skin: 'layui-btn layui-btn-primary layui-btn-sm layui-btn-radius',
                tagText: '<i class="layui-icon layui-icon-add-1"></i>添加标签'
            });
            laydate.render({
                elem: '#menuDate',
                value: new Date()
                , type: 'datetime'
                , trigger: 'click'
            });
            $.ajax({
                url: config.base_server + 'getMenutypeAll',
                dataType: 'json',
                type: 'get',
                async: false,
                success: function (res) {
                    data = res.data;
                    $.each(data, function (index, item) {
                        $('#menuType').append(new Option(item.menutypeTitle));
                    });
                    form.render("select");
                }
            })
            upload.render({
                elem: '#uploadBtn'
                , multiple: true
                , bindAction: "#save"
                , auto: false
                , choose: (obj) => {//预览图片
                    files = obj.pushFile();
                    obj.preview((index, file, result) => {
                        imgUrl = result
                        $('#previewBox').append('<div class="imgBox" ><img src="' + result + '" alt="' + file.name + '" class="layui-upload-img" width="100" height="100"><a class="remove_' + index + ' imgitem">取消</a></div>')
                        $('.remove_' + index).bind('click', function () {
                            delete files[index];//删除指定图片
                            $(this).parent().remove();
                        })
                    });
                }
            });

            layedit.set({

                uploadImage: {
                    url: config.base_server + 'menuMaterialsuploadImg',
                    accept: 'image',
                    acceptMime: 'image/*',
                    exts: 'jpg|png|gif|bmp|jpeg',
                    size: '10240'
                }
                //右键删除图片/视频时的回调参数，post到后台删除服务器文件等操作，
                //传递参数：
                //图片： imgpath --图片路径
                //视频： filepath --视频路径 imgpath --封面路径
                , calldel: {
                    url: '/Attachment/DeleteFile'
                }
                //开发者模式 --默认为false
                , devmode: true
                //插入代码设置
                , codeConfig: {
                    hide: true,  //是否显示编码语言选择框
                    default: 'javascript' //hide为true时的默认语言格式
                }
                , tool: [
                    'html', 'code', 'strong', 'italic', 'underline', 'del', 'addhr', '|', 'fontFomatt', 'colorpicker', 'face'
                    , '|', 'left', 'center', 'right', '|', 'image_alt', '|', 'fullScreen'
                ]
                , height: '400px'
            });

            var ieditor = layedit.build('menuMaterials');


            $('#submit').on('click', function () {
                layedit.sync(ieditor)
                var data = form.val('example');
                var { menuName, menuType, menuPrice, menuTime, menuDate, menuComm, menuMaterials, menuFlavor, menuHot } = data
                var menuTag = $.makeArray($('.tag[lay-filter="demotag"]>.tag-item').map(function () {
                    return $(this).text()
                }))
                var formData = new FormData();
                for (var i = 0; i < Object.values(files).length; i++) {
                    formData.append('menuImg', Object.values(files)[i]);
                }
                formData.append('menuName', menuName);
                formData.append('menuPrice', menuPrice);
                formData.append('menuTag', JSON.stringify(menuTag));
                formData.append('menuDate', menuDate);
                formData.append('menuComm', menuComm);
                formData.append('menuType', menuType);
                formData.append('menuHot', menuHot);
                formData.append('menuTime', menuTime);
                formData.append('menuFlavor', menuFlavor);
                formData.append('menuMaterials', menuMaterials)
                $.ajax({
                    type: "POST",
                    url: config.base_server + "addMenu",
                    async: false,
                    data: formData,
                    processData: false,   //  告诉jquery不要处理发送的数据
                    contentType: false,
                    success: function (data) {
                        if (data.code == 1) {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            alert("添加成功");
                            parent.layer.close(index); //再执行关闭   
                        } else {
                            alert(data.msg);
                        }
                    },
                    error() {
                        alert("文件太大")
                    }
                });
                return false;
            });
        })
    </script>


</body>